package my.jwds.p414;

public class ThirdMax {
    public static void main(String[] args) {
        ThirdMax obj = new ThirdMax();
        int nums[] = {1,2,Integer.MIN_VALUE};
        System.out.println(obj.thirdMax(nums));
    }

    public int thirdMax(int[] nums) {
        int[]maxes = new int[2];
        int max = Integer.MIN_VALUE;
        for (int num : nums) {
            if (num >max){
                max = num;
            }
        }
        maxes[0] = max;
        max = Integer.MIN_VALUE;
        for (int num : nums) {
            if (num >max && num != maxes[0]){
                max = num;
            }
        }
        maxes[1] = max;
        max = Integer.MIN_VALUE;
        boolean flag = false;
        for (int num : nums) {
            if (num >=max && num != maxes[0] && num !=maxes[1]){
                max = num;
                flag = true;
            }
        }
        return flag? max:maxes[0];
    }
}